// import { lstat,readdir } from 'fs/promises';
// import * as fs from 'fs';
var fs = require('fs');//按照node的根目录使用相对路径就行了
// console.log({fs});
class DirTree{
  /**
   * 
   * @param {String} dir 传入需要读取的路径
   */
  constructor(dir){
    this.curDir=dir;
    this.curValue=this.getCurValue(dir),
    this.children=[];
    this.getChildTree(dir);
  }

  getCurValue(dir){
    let pos=dir.lastIndexOf('/');
    return dir.substring(pos + 1);
  }

  getChildTree(dir){
    if(!fs.lstat(dir).isDirectory()){//不是文件夹，直接返回
      return;
    }
    let files = fs.readdir(dir); //当前文件夹下的文件或者文件夹
    files.forEach(item=>{
      let cur = dir+'/'+item;
      let stat = fs.lstat(cur);
      if(stat.isDirectory()){ //文件夹
        let dirChild = new DirTree(cur);
        this.children.push(dirChild);
      }else if( stat.isFile() ){//文件
        let fileChild =new DirTree(cur);
        this.children.push(fileChild);
      }
    })
  }
}

let d =new DirTree(`./data/article/`);
console.log(d.children);
console.log(d.dirChild);

export {DirTree};
